/*
    SUSAN® - Sole of Unix Save ANything

   Copyright (C) 2016-2016 Skyatlas Co. CN
   Copyright (C) 2016-2019 Hechzh Property

   
*/
/*
 * Susan Catalog Database Query loading routines.
 *
 * Written by Marco van Wieringen, May 2016
 */

#include "include/susan.h"

#if HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES || HAVE_DBI

#include "cats.h"

static const int debuglevel = 100;

const char* SusanDb::get_predefined_query_name(SusanDb::SQL_QUERY query)
{
  return query_names[static_cast<int>(query)];
}

const char* SusanDb::get_predefined_query(SusanDb::SQL_QUERY query)
{
  if (!queries) {
    Emsg0(M_ERROR, 0, "No SQL queries defined. This should not happen.");
    return NULL;
  }

  return queries[static_cast<int>(query)];
}

void SusanDb::FillQuery(SusanDb::SQL_QUERY predefined_query, ...)
{
  va_list arg_ptr;

  va_start(arg_ptr, predefined_query);
  FillQueryVaList(cmd, predefined_query, arg_ptr);
  va_end(arg_ptr);
}

void SusanDb::FillQuery(PoolMem& query,
                         SusanDb::SQL_QUERY predefined_query,
                         ...)
{
  va_list arg_ptr;

  va_start(arg_ptr, predefined_query);
  FillQueryVaList(query, predefined_query, arg_ptr);
  va_end(arg_ptr);
}

void SusanDb::FillQuery(POOLMEM*& query,
                         SusanDb::SQL_QUERY predefined_query,
                         ...)
{
  va_list arg_ptr;

  va_start(arg_ptr, predefined_query);
  FillQueryVaList(query, predefined_query, arg_ptr);
  va_end(arg_ptr);
}


void SusanDb::FillQueryVaList(POOLMEM*& query,
                               SusanDb::SQL_QUERY predefined_query,
                               va_list arg_ptr)
{
  PoolMem query_tmp(PM_MESSAGE);

  FillQueryVaList(query_tmp, predefined_query, arg_ptr);
  PmMemcpy(query, query_tmp, query_tmp.strlen() + 1);
}


void SusanDb::FillQueryVaList(PoolMem& query,
                               SusanDb::SQL_QUERY predefined_query,
                               va_list arg_ptr)
{
  const char* query_name;
  const char* query_template;

  query_name = get_predefined_query_name(predefined_query);
  query_template = get_predefined_query(predefined_query);

  Dmsg3(debuglevel, "called: %s with query name %s (%d)\n", __PRETTY_FUNCTION__,
        query_name, predefined_query);

  if (query_template) { query.Bvsprintf(query_template, arg_ptr); }

  Dmsg2(debuglevel, "called: %s query is now %s\n", __PRETTY_FUNCTION__,
        query.c_str());
}


bool SusanDb::SqlQuery(SusanDb::SQL_QUERY predefined_query, ...)
{
  va_list arg_ptr;
  PoolMem query(PM_MESSAGE);

  va_start(arg_ptr, predefined_query);
  FillQueryVaList(query, predefined_query, arg_ptr);
  va_end(arg_ptr);

  return SqlQuery(query.c_str());
}


bool SusanDb::SqlQuery(const char* query, int flags)
{
  bool retval;

  Dmsg2(debuglevel, "called: %s with query %s\n", __PRETTY_FUNCTION__, query);

  DbLock(this);
  retval = SqlQueryWithoutHandler(query, flags);
  if (!retval) {
    Mmsg(errmsg, _("Query failed: %s: ERR=%s\n"), query, sql_strerror());
  }
  DbUnlock(this);

  return retval;
}

bool SusanDb::SqlQuery(const char* query,
                        DB_RESULT_HANDLER* ResultHandler,
                        void* ctx)
{
  bool retval;

  Dmsg2(debuglevel, "called: %s with query %s\n", __PRETTY_FUNCTION__, query);

  DbLock(this);
  retval = SqlQueryWithHandler(query, ResultHandler, ctx);
  if (!retval) {
    Mmsg(errmsg, _("Query failed: %s: ERR=%s\n"), query, sql_strerror());
  }
  DbUnlock(this);

  return retval;
}
#endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES || \
          HAVE_DBI */
